/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Client.java
 *
 * Created on 21-nov-2011, 18:51:35
 */
package edu.uoc.tdp.pac4.only5.gui.client;

import edu.uoc.tdp.pac4.only5.domain.EnumRole;
import edu.uoc.tdp.pac4.only5.gui.client.panels.connexio.ConsultaAulesPanel;
import edu.uoc.tdp.pac4.only5.gui.client.panels.connexio.ConsultaReservesPanel;
import edu.uoc.tdp.pac4.only5.gui.client.panels.connexio.HistoricDespesesPanel;
import edu.uoc.tdp.pac4.only5.manager.BookingManager;
import edu.uoc.tdp.pac4.only5.manager.PaymentManager;
import edu.uoc.tdp.pac4.only5.manager.ResourceManager;
import edu.uoc.tdp.pac4.only5.manager.SpaceManager;
import edu.uoc.tdp.pac4.only5.manager.UserManager;
import edu.uoc.tdp.pac4.only5.manager.impl.BookingManagerImpl;
import edu.uoc.tdp.pac4.only5.manager.impl.DataBaseConnectionManagerImpl;
import edu.uoc.tdp.pac4.only5.manager.impl.PaymentManagerImpl;
import edu.uoc.tdp.pac4.only5.manager.impl.ResourceManagerImpl;
import edu.uoc.tdp.pac4.only5.manager.impl.SpaceManagerImpl;
import edu.uoc.tdp.pac4.only5.manager.impl.UserManagerImpl;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.rmi.RemoteException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Gerard
 */
public class ConnexioFrame extends javax.swing.JFrame {

    private static ResourceBundle resourceBundle;
    
    /*
     * PANELS PROPIS CLIENT:
     */
    
    private ConsultaReservesPanel consultaReservesPanel;
    private HistoricDespesesPanel historicDespesesPanel;
    private ConsultaAulesPanel consultaAulesPanel;
    
    private BookingManager bookingManager;
    private SpaceManager spaceManager;
    private ResourceManager resourceManager;
    private PaymentManager paymentManager;
    private UserManager userManager;
   

    /** Creates new form Client */
    public ConnexioFrame() {
        initComponents();
        this.setLayout(new FlowLayout());
        resourceBundle = ResourceBundle.getBundle("edu.uoc.tdp.pac4.only5.gui.client.Client");
        setLabels();
        configureViewDependsOfRole();
        try {
            bookingManager = new BookingManagerImpl(DataBaseConnectionManagerImpl.getInstance());
            spaceManager = new SpaceManagerImpl(DataBaseConnectionManagerImpl.getInstance());
            resourceManager = new ResourceManagerImpl(DataBaseConnectionManagerImpl.getInstance());
            paymentManager = new PaymentManagerImpl(DataBaseConnectionManagerImpl.getInstance());
            userManager = new UserManagerImpl(DataBaseConnectionManagerImpl.getInstance());
        } catch (RemoteException ex) {
            //TODO: handle errors!!!
            Logger.getLogger(ConnexioFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        mnu_gestioreserves = new javax.swing.JMenu();
        mnu_gestioreserves_consultareserves = new javax.swing.JMenuItem();
        mnu_gestioreserves_reservaaules = new javax.swing.JMenuItem();
        mnu_gestioreserves_cancelacioaules = new javax.swing.JMenuItem();
        mnu_gestioreserves_historicdespeses = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        mnu_gestioreserves.setText("Gestio de reserves d'aules i recursos");
        mnu_gestioreserves.setEnabled(false);
        mnu_gestioreserves.setName("mnu_gestioreserves"); // NOI18N

        mnu_gestioreserves_consultareserves.setText("Consulta de reserves");
        mnu_gestioreserves_consultareserves.setName("mnu_gestioreserves_consultareserves"); // NOI18N
        mnu_gestioreserves_consultareserves.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnu_gestioreserves_consultareservesActionPerformed(evt);
            }
        });
        mnu_gestioreserves.add(mnu_gestioreserves_consultareserves);

        mnu_gestioreserves_reservaaules.setText("Reserva d'aules");
        mnu_gestioreserves_reservaaules.setName("mnu_gestioreserves_reservaaules"); // NOI18N
        mnu_gestioreserves_reservaaules.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnu_gestioreserves_reservaaulesActionPerformed(evt);
            }
        });
        mnu_gestioreserves.add(mnu_gestioreserves_reservaaules);

        mnu_gestioreserves_cancelacioaules.setText("Cancel·lació aules");
        mnu_gestioreserves_cancelacioaules.setName("mnu_gestioreserves_cancelacioaules"); // NOI18N
        mnu_gestioreserves_cancelacioaules.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnu_gestioreserves_cancelacioaulesActionPerformed(evt);
            }
        });
        mnu_gestioreserves.add(mnu_gestioreserves_cancelacioaules);

        mnu_gestioreserves_historicdespeses.setText("Històric de despeses reserves i recàrrecs");
        mnu_gestioreserves_historicdespeses.setName("mnu_gestioreserves_historicdespeses"); // NOI18N
        mnu_gestioreserves_historicdespeses.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnu_gestioreserves_historicdespesesActionPerformed(evt);
            }
        });
        mnu_gestioreserves.add(mnu_gestioreserves_historicdespeses);

        jMenuBar1.add(mnu_gestioreserves);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 605, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 278, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void mnu_gestioreserves_consultareservesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnu_gestioreserves_consultareservesActionPerformed
    desactivarPanels();
    consultaReservesPanel = new ConsultaReservesPanel(bookingManager,spaceManager,paymentManager, userManager, resourceManager, this);    
    this.add(consultaReservesPanel, BorderLayout.CENTER);
    this.pack();
}//GEN-LAST:event_mnu_gestioreserves_consultareservesActionPerformed

private void mnu_gestioreserves_reservaaulesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnu_gestioreserves_reservaaulesActionPerformed
    desactivarPanels();
//    consultaAulesPanel = new ConsultaAulesPanel(this, spaceManager, bookingManager,resourceManager);
    consultaAulesPanel.setVisible(true);
    this.add(consultaAulesPanel, BorderLayout.CENTER);
    this.pack();
}//GEN-LAST:event_mnu_gestioreserves_reservaaulesActionPerformed

private void mnu_gestioreserves_cancelacioaulesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnu_gestioreserves_cancelacioaulesActionPerformed
    
}//GEN-LAST:event_mnu_gestioreserves_cancelacioaulesActionPerformed

private void mnu_gestioreserves_historicdespesesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnu_gestioreserves_historicdespesesActionPerformed
    desactivarPanels();
    historicDespesesPanel = new HistoricDespesesPanel(bookingManager);
    this.add(historicDespesesPanel, BorderLayout.CENTER);
    this.pack();
}//GEN-LAST:event_mnu_gestioreserves_historicdespesesActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ConnexioFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ConnexioFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ConnexioFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ConnexioFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ConnexioFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu mnu_gestioreserves;
    private javax.swing.JMenuItem mnu_gestioreserves_cancelacioaules;
    private javax.swing.JMenuItem mnu_gestioreserves_consultareserves;
    private javax.swing.JMenuItem mnu_gestioreserves_historicdespeses;
    private javax.swing.JMenuItem mnu_gestioreserves_reservaaules;
    // End of variables declaration//GEN-END:variables

    private void setLabels() {
        mnu_gestioreserves.setText(getText(mnu_gestioreserves.getName()));
        mnu_gestioreserves_cancelacioaules.setText(getText(mnu_gestioreserves_cancelacioaules.getName()));
        mnu_gestioreserves_consultareserves.setText(getText(mnu_gestioreserves_consultareserves.getName()));
        mnu_gestioreserves_historicdespeses.setText(getText(mnu_gestioreserves_historicdespeses.getName()));
        mnu_gestioreserves_reservaaules.setText(getText(mnu_gestioreserves_reservaaules.getName()));
    }

    private String getText(String key) {
        return resourceBundle.getString(key);
    }

    private void desactivarPanels() {
        if(ClientSession.getInstance().getRole() != null){
            if(ClientSession.getInstance().getRole().equals(EnumRole.ADMINISTRADOR))
                mnu_gestioreserves.setEnabled(true);
        }
        for(Component component : this.getContentPane().getComponents()){
            try {
                this.remove(component);
            } catch (Exception e) {
            }
        }
    }

    private void configureViewDependsOfRole() {
        EnumRole role = ClientSession.getInstance().getRole();
        
        if(role.equals(EnumRole.ADMINISTRADOR)){
            mnu_gestioreserves.setEnabled(true);
        }
        
        
    }
}
